package com.huiyujiaoyu.powertime.ui.my;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.huiyujiaoyu.base.BaseFragment;
import com.huiyujiaoyu.base.WebViewActivity;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.LoginBean;
import com.huiyujiaoyu.powertime.model.OptionItem;
import com.huiyujiaoyu.powertime.model.UserInfoBean;
import com.huiyujiaoyu.util.Constant;
import com.huiyujiaoyu.util.DisplayUtil;
import com.huiyujiaoyu.util.GlideUtil;
import com.huiyujiaoyu.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 首页
 * Created by yujunlong on 2016/12/5.
 */

public class MyFragment extends BaseFragment {


    @BindView(R.id.iv_have_info)
    ImageView mIvHaveInfo;
    @BindView(R.id.iv_msg_circle)
    ImageView mIvMsgCircle;
    @BindView(R.id.fl_msg)
    FrameLayout mFlMsg;
    @BindView(R.id.cir_icon)
    ImageView mCirIcon;
    @BindView(R.id.tv_nickname)
    TextView mTvNickname;
    @BindView(R.id.tv_desc)
    TextView mTvDesc;
    @BindView(R.id.ll_personal_info)
    LinearLayout mLlPersonalInfo;
    @BindView(R.id.tv_dingyue)
    TextView mTvDingyue;
    @BindView(R.id.tv_shoucang)
    TextView mTvShoucang;
    @BindView(R.id.tv_chengzhang)
    TextView mTvChengzhang;
    @BindView(R.id.rv_content)
    RecyclerView mRvContent;

    private BaseQuickAdapter<OptionItem, BaseViewHolder> adapter;

    private String[] titles;

    private List<OptionItem> datas;
    public static MyFragment newInstance() {
        Bundle args = new Bundle();
        MyFragment fragment = new MyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    protected void userBundle(Bundle savedInstanceState) {
        super.userBundle(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleNoBar(R.color.test_1);
        // setTitleNoBar();
        HttpMethods.getInstance().userinfo(mContext, getComp(), this);
    }

    @Override
    protected void initData() {
        super.initData();
        datas = new ArrayList<>();
        titles = new String[]{"我的VIP","账户","我的积分","我的优惠券","成为作者","账号","设置"};
        datas.add(new OptionItem(R.mipmap.icon_personal_wodevip,titles[0],UserInfoBean.getInstance().getExpiry_date()));
        datas.add(new OptionItem(R.mipmap.icon_personal_wodevip,titles[1],""));
        datas.add(new OptionItem(R.mipmap.icon_personal_wodejifen,titles[2],""));
        datas.add(new OptionItem(R.mipmap.icon_personal_wodeyouhuiquan,titles[3],""));

        datas.add(new OptionItem(R.mipmap.icon_personal_chenngweizuozhe,titles[4],""));
        datas.add(new OptionItem(R.mipmap.icon_personal_zhanghao,titles[5],""));
        datas.add(new OptionItem(R.mipmap.icon_personal_shezhi,titles[6],""));
        adapter = new BaseQuickAdapter<OptionItem, BaseViewHolder>(R.layout.cell_modleitem,datas) {
            @Override
            protected void convert(BaseViewHolder helper, OptionItem item) {
                   TextView item_one =  helper.getView(R.id.tv_item_one);
                    helper.setText(R.id.tv_item_two,item.right);
                   DisplayUtil.setDrawbleLeft(mContext,item_one,item.lrsdid,10);
                   item_one.setText(item.ltext);
                    StringUtils.setVisibility(helper.itemView,item.isOk);
            }
        };
        LinearLayoutManager layoutManager =  new LinearLayoutManager(mContext);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);

        mRvContent.setLayoutManager(layoutManager);
        mRvContent.setHasFixedSize(true);
        mRvContent.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        mTvNickname.setText(UserInfoBean.getInstance().getNickName());
        mTvDesc.setText(UserInfoBean.getInstance().getSignature());
//        mTvVip.setText(UserInfoBean.getInstance().getExpiry_date());
        GlideUtil.loadHeaderImg(mCirIcon, StringUtils.getFullUrl(UserInfoBean.getInstance().getHeadImg()));
    }

    @Override
    protected void initListener() {
        super.initListener();
        mRvContent.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                OptionItem indexItemModel = datas.get(position);
                 if(indexItemModel.ltext.equals(titles[0])){
                     if ("1".contains(UserInfoBean.getInstance().getIsVip())) {
                         MyVip2Activity.launch(mContext);
                     } else {
                         MyVipActivity.launch(mContext);
                     }
                 }else if(indexItemModel.ltext.equals(titles[1])){
                     MyVipPayActivity.launch(mContext);
                 }else if(indexItemModel.ltext.equals(titles[2])){
                     MyJiFenActivity.launch(mContext);
                 }else if(indexItemModel.ltext.equals(titles[3])){
                     MyYHQActivity.launch(mContext);
                 }else if(indexItemModel.ltext.equals(titles[4])){
                     WebViewActivity.startActivity(mContext,"成为作者", Constant.JOIN_AU,1);
                 }else if(indexItemModel.ltext.equals(titles[5])){
                      MyAccountActivity.launch(mContext);
                 }else if(indexItemModel.ltext.equals(titles[6])){
                     SettingActivity.launch(mContext);
                 }
            }
        });
    }

    @OnClick({R.id.fl_msg, R.id.ll_personal_info, R.id.tv_dingyue,
            R.id.tv_shoucang, R.id.tv_chengzhang})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_msg:
                MessagerCenterActivity.launch(mContext);
                break;
            case R.id.ll_personal_info:
                PersnonalInfoActivity.launch(mContext);
                break;
            case R.id.tv_dingyue:
                BaseListActivity.launch(mContext, 1);
                break;
            case R.id.tv_shoucang:
                MyCollectActivity.launch(mContext);
                break;
        }
    }

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_USERINFO:
                LoginBean bean = (LoginBean) o;
                UserInfoBean.getInstance().setAccess(bean);
                break;
        }
    }

}
